- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathKnapsack.java
118 lines (88 loc) Β· 3.26 KB
/
Knapsack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
packagesection19_DynamicProgramming;
publicclassKnapsack {
publicstaticvoidmain(String[] args) {
int[] weight = { 1, 3, 4, 5 };
int[] price = { 1, 4, 5, 7 };
intbagCapacity = 7;
intvirtualIndex = 0;
int[][] strg = newint[weight.length][bagCapacity + 1];
System.out.println(knapsack01Rec(weight, price, bagCapacity, virtualIndex)); // 9
System.out.println(knapsackTopDown(weight, price, bagCapacity, virtualIndex, strg)); // 9
System.out.println(knapsackBottomUpDP(weight, price, bagCapacity)); // 9
System.out.println("\ntesting large inputs...\n" + "");
intn = 1000;
int[] wt = newint[n];
int[] p = newint[n];
intcap = 70;
for (inti = 0; i < n; i++) {
wt[i] = i + 1;
p[i] = i + 2;
}
// using DP
int[][] storage = newint[wt.length][cap + 1];
longstart = System.currentTimeMillis();
// System.out.println(knapsack01Rec(wt, p, cap, 0));
System.out.println(knapsackTopDown(wt, p, cap, 0, storage));
longend = System.currentTimeMillis();
System.out.println("time taken by top down: " + (end - start) + " msec");
start = System.currentTimeMillis();
System.out.println(knapsackBottomUpDP(wt, p, cap));
end = System.currentTimeMillis();
System.out.println("time taken by bottom up: " + (end - start) + " msec");
}
/*
* Big O analysis shortcut for recursive solution-
*
* time = no. of function frames * time taken by 1 function frame
*
* no. of function frame = total calls ^ (recursion tree height)
*/
// O(2^n) Time, n = total items in bag
publicstaticintknapsack01Rec(int[] weight, int[] price, intcapacity, intvidx) {
/*
* 1. if no item left
*
* 2. if current capacity is less than item weight to be included
*/
if (vidx == weight.length || capacity < weight[vidx]) {
return0;
}
// exclude item
intexcludeProfit = knapsack01Rec(weight, price, capacity, vidx + 1);
// include item
intincludeProfit = price[vidx] + knapsack01Rec(weight, price, capacity - weight[vidx], vidx + 1);
intprofit = Math.max(excludeProfit, includeProfit);
returnprofit;
}
publicstaticintknapsackTopDown(int[] weight, int[] price, intcapacity, intvidx, int[][] storage) {
if (vidx == weight.length || capacity == 0) {
return0;
}
if (storage[vidx][capacity] != 0) {
returnstorage[vidx][capacity];
}
intexcludeProfit = knapsackTopDown(weight, price, capacity, vidx + 1, storage);
intincludeProfit = 0;
if (capacity >= weight[vidx])
includeProfit = price[vidx] + knapsackTopDown(weight, price, capacity - weight[vidx], vidx + 1, storage);
intprofit = Math.max(excludeProfit, includeProfit);
storage[vidx][capacity] = profit;
returnprofit;
}
// O(NM) Time, N is total items, M is total capacity | O(NM) Space
publicstaticintknapsackBottomUpDP(int[] weight, int[] price, intcapacity) {
int[][] storage = newint[weight.length + 1][capacity + 1];
// filling bottom up, left to right
for (introw = weight.length - 1; row >= 0; row--) {
for (intcol = 1; col <= capacity; col++) {
intexclude = storage[row + 1][col];
intinclude = 0;
if (col >= weight[row])
include = storage[row + 1][col - weight[row]] + price[row];
intans = Math.max(exclude, include);
storage[row][col] = ans;
}
}
returnstorage[0][capacity];
}
}